#include<bits/stdc++.h>
using namespace std;
namespace grm {
  /**
   * @brief Quick Modular Exponentiation O(log(n))
   * 
   * @param x
   * @param n 
   * @param m 
   * @return int (x**n)%m
   */
  int modpow(int x, int n, int m) {
    if(n==0) {return 1%m;}
    long long u = modpow(x, n/2, m);
    u = (u*u)%m;
    if(n%2==1) {u=(u*x)%m;}
    return u;
  }
}

int main() {
  int x = 4321, n=6162, m = 631;
  cout<<grm::modpow(x, n, m)<<endl;
}